package com.net.xpay.common.validator;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.notification.Notification;
import com.net.xpay.common.enums.notification.NotificationStatus;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.common.exception.BusinessException;
import com.net.common.util.SerializationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class NotificationValidator {

    private static final int MAX_TITLE_SIZE = 32;

    private static final int MAX_SUMMARY_SIZE = 100;

    private static final int MAX_CONTENT_SIZE = 65535;

    public void validateAddNotification(Notification notification) {
        log.info("validateAddNotification params={}", SerializationUtil.obj2String(notification));

        if (notification == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公告不能为空");
        }

        if (Strings.isNullOrEmpty(notification.getTitle())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "标题不能为空");
        }
        if (notification.getTitle().length() > MAX_TITLE_SIZE) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "标题不能超过" + MAX_TITLE_SIZE + "个字");
        }
        if (Strings.isNullOrEmpty(notification.getSummary())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "摘要不能为空");
        }
        if (notification.getSummary().length() > MAX_SUMMARY_SIZE) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "摘要不能超过" + MAX_SUMMARY_SIZE + "个字");
        }
        if (Strings.isNullOrEmpty(notification.getContent())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "内容不能为空");
        }
        if (notification.getContent().length() > MAX_CONTENT_SIZE) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "内容不能超过" + MAX_CONTENT_SIZE + "个字");
        }

        if (notification.getStartTime() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开始时间不能为空");
        }
        if (notification.getEndTime() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "结束时间不能为空");
        }
        if (notification.getStartTime().isAfter(notification.getEndTime())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开始时间必须小于结束时间");
        }
        if (notification.getEndTime().isBefore(LocalDateTime.now())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "结束时间必须大于当前时间");
        }
        if (notification.getStatus() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公告状态不能为空");
        }
        if (notification.getStatus() == NotificationStatus.GARBAGE) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公告状态不能为" + NotificationStatus.GARBAGE.getValue());
        }
    }

    public void validateUpdateNotification(Notification notification) {
        log.info("validateUpdateNotification params={}", SerializationUtil.obj2String(notification));

        if (notification == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公告不能为空");
        }
        if (notification.getId() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公告id不能为空");
        }

        if (Strings.isNullOrEmpty(notification.getTitle())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "标题不能为空");
        }
        if (notification.getTitle().length() > MAX_TITLE_SIZE) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "标题不能超过" + MAX_TITLE_SIZE + "个字");
        }
        if (Strings.isNullOrEmpty(notification.getSummary())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "摘要不能为空");
        }
        if (notification.getSummary().length() > MAX_SUMMARY_SIZE) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "摘要不能超过" + MAX_SUMMARY_SIZE + "个字");
        }
        if (Strings.isNullOrEmpty(notification.getContent())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "内容不能为空");
        }
        if (notification.getContent().length() > MAX_CONTENT_SIZE) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "内容不能超过" + MAX_CONTENT_SIZE + "个字");
        }

        if (notification.getStartTime() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开始时间不能为空");
        }
        if (notification.getEndTime() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "结束时间不能为空");
        }
        if (notification.getStartTime().isAfter(notification.getEndTime())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "开始时间必须小于结束时间");
        }
        if (notification.getEndTime().isBefore(LocalDateTime.now())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "结束时间必须大于当前时间");
        }
        if (notification.getStatus() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公告状态不能为空");
        }
        if (notification.getStatus() == NotificationStatus.GARBAGE) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "公告状态不能为" + NotificationStatus.GARBAGE.getValue());
        }
    }
}
